package edu.faytechcc.csc.gui.search;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JToolBar;

/**
 * Creates a Menubar
 *
 * @author Stephanie Mckenzie and Jasmin Pastari
 */
public class CreateARGMenuBar
{

    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JToolBar argToolBar;
    private JRadioButtonMenuItem mangaItem;
    private JRadioButtonMenuItem animeItem;
    private ButtonGroup selectButtonGroup;
    private String tableName = "Anime_Table";
    private SearchPanel search;
    private ListPanel list;

    /**
     * Constructor for the Menubar
     * 
     * @param search
     * @param list
     */
    public CreateARGMenuBar(SearchPanel search,
            ListPanel list)
    {
        // sets the SearchPanel
        this.search = search;
        // sets the ListPanel
        this.list = list;
        // fill the empty variables
        menuBar = new JMenuBar();
        argToolBar = new JToolBar();
        fileMenu = new JMenu("File");
        mangaItem = new JRadioButtonMenuItem("Manga");
        animeItem = new JRadioButtonMenuItem("Anime", true);
        selectButtonGroup = new ButtonGroup();
        argToolBar.setFloatable(false);// makes it so you can't move it around
        //add actionlisteners
        mangaItem.addActionListener(new itemActionListener());
        animeItem.addActionListener(new itemActionListener());

        //set the feile menues mnemonic
        fileMenu.setMnemonic(KeyEvent.VK_F);
        mangaItem.setMnemonic(KeyEvent.VK_M);
        animeItem.setMnemonic(KeyEvent.VK_A);

        // add manga and anime buttons to button group and file menu
        selectButtonGroup.add(mangaItem);
        selectButtonGroup.add(animeItem);
        fileMenu.add(mangaItem);
        fileMenu.add(animeItem);

        // add menu's and items to the menubar
        menuBar.add(fileMenu);
        menuBar.add(argToolBar);
        search.setTable(tableName);
    }

    /**
     *
     * @return menuBar
     */
    public JMenuBar getFileMenu()
    {
        return menuBar;
    }

    /**
     * When the menubar items are changed from manga to anime it will send
     * a message to the search and list panels that it's been changed.
     */
    private class itemActionListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (mangaItem.isSelected())
            {
                tableName = "Manga_Table";
            } else
            {
                tableName = "Anime_Table";
            }
            search.setTable(tableName);
            list.setTableName(tableName);
        }
    }
}
